package com.zhuannimei.ui;

import java.util.Set;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import com.umeng.update.UmengUpdateAgent;
import com.zhuannimei.R;
import com.zhuannimei.utility.UserData;
import com.zhuannimei.utility.WuLog;

public class ConversionActivity extends TabActivity implements OnCheckedChangeListener{
	private static final String TAG = "ConversionActivity";
	private TabHost mHost;
	private RadioGroup radioderGroup;
	private String userUserId;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.maintabs);
        UmengUpdateAgent.setUpdateOnlyWifi(false);
        UmengUpdateAgent.update(this);
        mHost=this.getTabHost();
        mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE")
        			.setContent(new Intent(this,TabOneActivity.class)));
        mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO")
        		.setContent(new Intent(this,OneYuanShopActivity.class)));
        mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE")
        		.setContent(new Intent(this,TabTwoActivity.class)));
        mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR")
        		.setContent(new Intent(this,TabFourActivity.class)));
        
        radioderGroup = (RadioGroup) findViewById(R.id.main_radio);
		radioderGroup.setOnCheckedChangeListener(this);
		mHost.setCurrentTab(0);
		RadioButton rb =(RadioButton) radioderGroup.findViewById(R.id.radio_button0);
		rb.setChecked(true);
		userUserId = UserData.getInstance().getUserUserId();
		WuLog.i(TAG, "用户id"+userUserId);
		if(!TextUtils.isEmpty(userUserId)){
			setAliasToJpush();
		}
	}
    private void setAliasToJpush(){
		JPushInterface.setAliasAndTags(ConversionActivity.this, userUserId, null, new TagAliasCallback(
				) {
			@Override
			public void gotResult(int arg0, String arg1, Set<String> arg2) {
				WuLog.i(TAG, arg0+"");
				if(arg0!=0){
					SystemClock.sleep(200);
					WuLog.i(TAG, "注册别名到极光服务器:"+arg0);
					setAliasToJpush();
				}
			}
		});
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch(checkedId){
		case R.id.radio_button0:
			mHost.setCurrentTabByTag("ONE");
			break;
		case R.id.radio_button1:
			mHost.setCurrentTabByTag("TWO");
			break;
		case R.id.radio_button2:
			mHost.setCurrentTabByTag("THREE");
			break;
		case R.id.radio_button3:
			mHost.setCurrentTabByTag("FOUR");
			break;
		}		
	}
}